<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-15 00:14
 *
 * 项目：rm  -  $  - viewValidations.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\table\form\FormData;

use Lev;
use lev\helpers\ScoreHelper;
use lev\helpers\UserCacheHelper;
use modules\levfm\helpers\UrlfmHelper;
use modules\levfm\table\LevfmForms;

!defined('INLEV') && exit('Access Denied LEV');


class viewValidations
{

    public static $dosubmit = false;
    public static $info = null;
    public static $formInfo = null;
    public static $accessInputs = null;

    public static function errorMessage(array $info, array $formInfo)
    {
        if (!$formInfo || $formInfo['status']) {
            return Lev::responseMsg(-1, '抱歉，数据表单不存在或已关闭');
        }

        $settings = $formInfo['settings'] = Lev::getSettings($formInfo['settings']);

        static::$info = $info;
        static::$formInfo = $formInfo;

        static::$accessInputs = LevfmForms::getAccessInputs();//print_r(static::$accessInputs);
        foreach (static::$accessInputs as $v) {
            if (method_exists(__CLASS__, $method = 'ck_'.$v['inputname'])) {
                $setval = $settings[formValidations::getSettingsKey($v['inputname'])];
                if ($errMsg = static::$method($setval)) {
                    return $errMsg;
                }
            }
        }
    }

    public static function ck_settings___payview($setval) {
        if (($setval = json_decode($setval, true)) && $acscore = floatval($setval[0])) {
            $cacheKey = 'ck_settings___payview'.static::$info['id'];
            if (!UserCacheHelper::getUserSetting(Lev::$app['uid'], $cacheKey)) {
                $notice = '【' . static::$formInfo['name'] . '】查阅信息 ';
                if (!Lev::GPv('payview')) {
                    Lev::showMessages(
                        Lev::responseMsg(-606001, $notice.'！需要支付：'.$acscore.ScoreHelper::scorename($setval[1])),
                        -1, Lev::toCurrent(['payview'=>1], true), 'submit'
                    );
                }
                if ($acMsg = ScoreHelper::acscoreUse(-$acscore, $notice, $setval[1])) {
                    if ($acMsg['status'] < 0) {
                        return $acMsg;
                    }
                    Lev::setNotices($notice . $acMsg['message']);
                    UserCacheHelper::setUserSetting(Lev::$app['uid'], $cacheKey, $acscore);
                }
            }
        }
    }

}